package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //只能获取自己的购物车
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        log.info("当前用户id：id={}",currentId);

        //判断当前商品是否在购物车中
        //查询购物车数据，条件：用户id+套餐id（菜品id+口味）
        ShoppingCart carDB = shoppingCartMapper.list(shoppingCart);

        if (carDB != null){
            //如果已经存在，就更新数量，数量加1
            carDB.setNumber(carDB.getNumber() + 1);
            shoppingCartMapper.update(carDB);
        }
        //如果不存在，插入数据，数量就是1
        else {
            Long dishId = shoppingCart.getDishId();
            if (dishId != null){    //插入的是菜品
                //添加到购物车的是菜品
                DishVO dishVO = dishMapper.selectById(dishId);
                shoppingCart.setName(dishVO.getName());
                shoppingCart.setImage(dishVO.getImage());
                shoppingCart.setDishId(dishVO.getId());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(dishVO.getPrice());
            }else {
                //添加到购物车的是套餐
                Long setmealId = shoppingCart.getSetmealId();
                Setmeal setmeal = setmealMapper.selectById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setSetmealId(setmeal.getId());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        //获取当前id购物车下的说有商品
        Long userId = BaseContext.getCurrentId();

        //调用mapper的查询
        return shoppingCartMapper.getByUserId(userId);
    }

    /**
     * 减少商品
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //获取当前用户的id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        //1.查询当前购物车该商品的数量
        ShoppingCart list = shoppingCartMapper.list(shoppingCart);

        //2.如果是1，则删除该商品
        Integer number = list.getNumber();
        if (number == 1){
            shoppingCartMapper.deleteByDishId(list.getDishId());
        }else {
            //3.否则将数量-1
            shoppingCart.setId(list.getId());
            shoppingCart.setNumber(number - 1);
            shoppingCartMapper.update(shoppingCart);
        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }


}
